package com.financing.platform.util;
import java.io.IOException;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.financing.platform.dto.FpNewProductDTO;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.ProxyConfig;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomText;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlBody;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlParagraph;
import com.gargoylesoftware.htmlunit.html.HtmlSpan;
import com.gargoylesoftware.htmlunit.html.HtmlStrong;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableBody;
import com.gargoylesoftware.htmlunit.html.HtmlTableDataCell;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class YicaifuWbClient {
	public static Logger log = Logger.getLogger(YicaifuWbClient.class);
	
	static HttpClient hc = null;
	final static PostMethod postmethod = new PostMethod();
	final static GetMethod getmethod = new GetMethod();
	final static List<String> hm=new ArrayList<String>();
	static WebClient wc=new WebClient(BrowserVersion.CHROME);
	
	
	//是否使用测试
	final static boolean flagTest=false;

	static class MyAuthenticator extends Authenticator {
		private String user = "";
		private String password = "";

		public MyAuthenticator(String user, String password) {
			this.user = user;
			this.password = password;
		}

		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(user, password.toCharArray());
		}
	}
	static {
//		 if(flagTest){//113.222.100.170:80,222.176.112.10:80,125.90.207.94:8080
//		    	wc=new WebClient(BrowserVersion.INTERNET_EXPLORER_8,"125.90.207.94", 8080);
//		    	DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) wc.getCredentialsProvider();
//		    	credentialsProvider.addCredentials("fefe4183","fefe1111");
//		}
		hc = new HttpClient();
		// 指定协议名称和默认的端口号
		Protocol myhttps = new Protocol("https",
				new MySecureProtocolSocketFactory(), 443);
		// 注册刚才创建的https 协议对象
		Protocol.registerProtocol("https", myhttps);
		hc.getParams().setParameter(HttpClientParams.USE_EXPECT_CONTINUE,
				Boolean.FALSE);
		// 设置超时限制
		hc.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
		hc.getHttpConnectionManager().getParams().setSoTimeout(5000);
		
//		hc.getHostConfiguration().setProxy("125.90.207.94", 8080);
		//IP代理限制
//		if(flagTest){
//			hc.getParams().setAuthenticationPreemptive(true);
//			Credentials usernamePasswordCredentials = new UsernamePasswordCredentials("fefe4183", "fefe1111");
//			hc.getState().setProxyCredentials(new AuthScope("115.47.41.246", 80, AuthScope.ANY_REALM), usernamePasswordCredentials);
//		}
		wc.getOptions().setThrowExceptionOnScriptError(false);
		wc.getOptions().setCssEnabled(false);
		wc.getOptions().setJavaScriptEnabled(false);
		wc.getOptions().setUseInsecureSSL(true);
		wc.setJavaScriptTimeout(50000);
		wc.getOptions().setTimeout(50000);
		wc.waitForBackgroundJavaScript(50000);
	    wc.getOptions().setThrowExceptionOnFailingStatusCode(false);  
	    
	}
	public static boolean getMemcache() {
		return false;
	}
	public static boolean getMemcacheBack() {
		boolean flag=false;
		//重新赋值       避免之前的 代理
		hc = new HttpClient();
		// 指定协议名称和默认的端口号
		Protocol myhttps = new Protocol("https",
				new MySecureProtocolSocketFactory(), 443);
		// 注册刚才创建的https 协议对象
		Protocol.registerProtocol("https", myhttps);
		hc.getParams().setParameter(HttpClientParams.USE_EXPECT_CONTINUE,
				Boolean.FALSE);
		// 设置超时限制
		hc.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
		hc.getHttpConnectionManager().getParams().setSoTimeout(5000);
		try {//  读取文件中的IP
			List<String> readLines = (List<String>) MemcacheCacheHelper.mcc.get("ipList");
			
			for (int i = readLines.size()-1; i >=0 ; i--) {
				String method=readLines.get(i);
				//设置代理
				ProxyConfig p=new ProxyConfig(method.split(":")[0], Integer.parseInt(method.split(":")[1]));
				wc.getOptions().setProxyConfig(p);
				HtmlPage page = wc.getPage("http://1212.ip138.com/ic.asp");
				System.out.println(page.asXml());
				//是否包含 内部IP    不包含  则  代理成功
				if(page.asXml().contains("42.121.125.242")||page.asXml().contains("192.168.1.88")||page.asXml().contains("203.156.233.216")||page.asXml().contains("121.40.238.29")){
					wc.closeAllWindows();
					log.info("getmemecached proxy error  remove: "+method.split(":")[0]+":"+Integer.parseInt(method.split(":")[1]));
					readLines.remove(i);
					continue;
				}
				log.info("getmemecached proxy success: "+method.split(":")[0]+":"+Integer.parseInt(method.split(":")[1]));
				//设置hc的代理
				hc.getHostConfiguration().setHost(method.split(":")[0], Integer.parseInt(method.split(":")[1]));
				flag=true;
				break;
			}
		} catch (Exception e) {
			log.error("get IP  error");
		} 
		if(!flag){
			try {
				fetchIp(20);
			} catch (Exception e) {
				log.error("fetchIp error");
			}
		}
		return flag;

	}
	
	//获取N个Ip  筛选有效     获取有效代理 并设置
	public static void fetchIp(int n) throws HttpException, IOException{
		try {
			if(n==0){
				return ;
			}
			log.info("get ip init:"+n);
			
			int num=1;//222.176.112.10:80,125.90.207.94:8080
			String fetchIpUrl="http://qsrdk.daili666api.com/ip/?tid=558137947253001&num="+num+"&operator=1&delay=1&protocol=https&foreign=none&filter=on";
			String method = getMethod(fetchIpUrl, null);
			log.info("proxy ip:"+method);
			//设置代理
			ProxyConfig p=new ProxyConfig(method.split(":")[0], Integer.parseInt(method.split(":")[1]));
			wc.getOptions().setProxyConfig(p);
			try {
				HtmlPage page = wc.getPage("http://1212.ip138.com/ic.asp");
				System.out.println(page.asXml());
				if(page.asXml().contains("42.121.125.242")||page.asXml().contains("192.168.1.88")||page.asXml().contains("203.156.233.216")){
					wc.closeAllWindows();
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
					}
					fetchIp(--n);
					return ;
				}
			} catch (Exception e1) {
				log.error("IP error "+method.split(":")[0]+":"+Integer.parseInt(method.split(":")[1]));
				wc.closeAllWindows();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
				}
				fetchIp(--n);
				return ;
			}
			log.info("proxy success: "+method.split(":")[0]+":"+Integer.parseInt(method.split(":")[1]));
			if(MemcacheCacheHelper.mcc.get("ipList")==null){
				MemcacheCacheHelper.mcc.set("ipList", new ArrayList<String>());
			}else{
				List<String> readLines = (List<String>) MemcacheCacheHelper.mcc.get("ipList");
				readLines.add(method.split(":")[0]+":"+Integer.parseInt(method.split(":")[1]));
			}
			//设置hc的代理
			hc.getHostConfiguration().setHost(method.split(":")[0], Integer.parseInt(method.split(":")[1]));
		} catch (Exception e) {
			log.error("get IP  error");
		}
		
	}
	
	
	public static String fecth(String xml, String regex, int group) {
		Pattern p = Pattern.compile(regex);
		String value = "";
		Matcher m = p.matcher(xml);
		if (m.find() && null != m.group(1)) {
			value = m.group(group);
		}
		return value.trim();
	}

	public static String getTimeStamp() {
		String randomNumber = "";
		for (int i = 0; i < 6; i++) {
			randomNumber += (int) (Math.random() * 10);
		}
		return System.currentTimeMillis() + randomNumber;
	}

	//cookie转换   通过 webClient处理结果交给   httpclient
	public static void webClientToHttp(HttpClient http, WebClient client)
			throws IOException {
		Set<com.gargoylesoftware.htmlunit.util.Cookie> cookies = client
				.getCookieManager().getCookies();
		for (com.gargoylesoftware.htmlunit.util.Cookie cookie : cookies) {
			Cookie cook = new Cookie(cookie.getDomain(), cookie.getName(),
					cookie.getValue());
			http.getState().addCookie(cook);
		}
	}
	//cookie转换   通过 webClient处理结果交给   httpclient
	public static void webClientToHttp()
			throws IOException {
		Set<com.gargoylesoftware.htmlunit.util.Cookie> cookies = wc
				.getCookieManager().getCookies();
		for (com.gargoylesoftware.htmlunit.util.Cookie cookie : cookies) {
			Cookie cook = new Cookie(cookie.getDomain(), cookie.getName(),
					cookie.getValue());
			hc.getState().addCookie(cook);
		}
	}
	@SuppressWarnings("unchecked")
	public static List<HtmlElement> getElementByJs(HtmlElement element, String selStr) {
		List<HtmlElement> els = (List<HtmlElement>) element.getByXPath(getXpathByCssSelect(selStr));
		if(els == null || els.isEmpty()) {
			return null;
		} else {
			return els;
		}
	}
	/**
	 * 将Css选择器 转换成Xpath选择器
	 * @param sel
	 * @return
	 */
	public static String getXpathByCssSelect(String sel) {
		String[] arrs = sel.split(" ");
		StringBuffer xpath = new StringBuffer("./");
		for (String str : arrs) {
			str = str.replace("@", " ");
			if(str.startsWith(".")) {
				xpath.append("/*[@class='" + str.substring(1) + "']");
			} else if (str.startsWith("#")) {
				xpath.append("/*[@id='" + str.substring(1) + "']");
			} else if (str.startsWith("[")) {
				xpath.append(str);
			} else {
				xpath.append("/" + str);
			}
		}
		return xpath.toString();
	}
	
	/**
	 * 
	
	* @Title: getText
	
	* @Description: 获取元素数据
	
	* @param @param licaishiinfo
	* @param @param xpath
	* @param @return    设定文件
	
	* @return String    返回类型
	
	* @throws
	 */
	public static String getText(HtmlElement licaishiinfo,String xpath){
		Object o=licaishiinfo.getFirstByXPath(xpath);
		if(o==null){
			return "";
		}else if(o instanceof HtmlStrong){
			return ((HtmlStrong)o).getTextContent().trim();
		}else if(o instanceof HtmlTextInput){
			return ((HtmlTextInput)o).getTextContent().trim();
		}else if(o instanceof HtmlSpan){
			return ((HtmlSpan)o).getTextContent().trim();
		}else if(o instanceof HtmlImage){
			return ((HtmlImage)o).getSrcAttribute().trim();
		}else if(o instanceof DomText){
			return ((DomText)o).getTextContent().trim();
		}else if(o instanceof HtmlDivision){
			return ((HtmlDivision)o).getTextContent().trim();
		}else if(o instanceof HtmlHeading1){
			return ((HtmlHeading1)o).getTextContent().trim();
		}else if(o instanceof HtmlAnchor){
			return ((HtmlAnchor)o).getTextContent().trim();
		}else if(o instanceof HtmlBody){
			return ((HtmlBody)o).getTextContent().trim();
		}else if(o instanceof HtmlTableDataCell){
			return ((HtmlTableDataCell)o).getTextContent().trim();
		}else if(o instanceof HtmlParagraph){
			return ((HtmlParagraph)o).getTextContent().trim();
		}else if(o instanceof HtmlTableBody){
			return ((HtmlTableBody)o).getTextContent().trim();
		}
		return ((String)o).trim();
	}
	//cookie转换   通过httpclient 处理结果交给   webClient
	public static void httpToWebClient(HttpClient http, WebClient client)
			throws IOException {
		for (Cookie cookie : http.getState().getCookies()) {
			com.gargoylesoftware.htmlunit.util.Cookie cook = new com.gargoylesoftware.htmlunit.util.Cookie(
					cookie.getDomain(), cookie.getName(), cookie.getValue());
			client.getCookieManager().addCookie(cook);
		}
	}
	//cookie转换   通过httpclient 处理结果交给   webClient
	public static void httpToWebClient()
			throws IOException {
		for (Cookie cookie : hc.getState().getCookies()) {
			com.gargoylesoftware.htmlunit.util.Cookie cook = new com.gargoylesoftware.htmlunit.util.Cookie(
					cookie.getDomain(), cookie.getName(), cookie.getValue());
			wc.getCookieManager().addCookie(cook);
		}
	}
	//print cookie
	public static void printCookie()
			throws IOException {
		for (Cookie cookie : hc.getState().getCookies()) {
			log.info(cookie.getName()+"="+cookie.getValue());
		}
	}
	public static String postMethod(String url, Map<String, String> params)
			throws IOException {
		PostMethod postmethod=new PostMethod(url);
		if (params != null && params.size() > 0) {
			Set<Entry<String, String>> set = params.entrySet();
			NameValuePair[] nvps = new NameValuePair[set.size()];
			int i = 0;
			for (Entry<String, String> entry : params.entrySet()) {
				nvps[i] = new NameValuePair(entry.getKey(), entry.getValue());
				i++;
			}
			postmethod.addParameters(nvps);
		}
		postmethod.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		int stat = hc.executeMethod(postmethod);
		log.info(stat);
		Header[] logins=postmethod.getResponseHeaders();
		if(stat==302){
			for (int i = 0; i < logins.length; i++) {
				log.info(logins[i].getName()+"==="+logins[i].getValue());
				if(logins[i].getName().equals("Location")&&StringUtils.isNotBlank(logins[i].getName())){
					log.info(logins[i].getValue());
					postmethod.releaseConnection();
					postMethod(logins[i].getValue(), null);
				}
			}
		}
		String str = postmethod.getResponseBodyAsString();
		postmethod.releaseConnection();
		return str;
	}
	public static String getMethod(String url, Map<String, String> params) throws HttpException, IOException {
		String str = "";
		if (params != null && params.size() > 0) {
			StringBuffer sb = new StringBuffer();
			Set<Entry<String, String>> set = params.entrySet();
			for (Iterator iterator = set.iterator(); iterator.hasNext();) {
				Entry<String, String> entry = (Entry<String, String>) iterator
						.next();
				sb.append("&" + entry.getKey() + "=" + entry.getValue());
			}
			if (url.indexOf("?") == -1) {
				sb.deleteCharAt(0);
				url = url + "?" + sb.toString();
			} else {
				url = url + sb.toString();
			}
		}
		GetMethod getmethod = new GetMethod(url);
//		getmethod.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
//		getmethod.setRequestHeader("Host","qsrdk.daili666api.com");
//		getmethod.setRequestHeader("Accept-Encoding","gzip, deflate, sdch");
//		getmethod.setRequestHeader("Connection","keep-alive");
//		getmethod.setRequestHeader("Pragma","no-cache");
//		getmethod.setRequestHeader("Upgrade-Insecure-Requests","1");
//		getmethod.setRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36");
//		getmethod.setRequestHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		int stat = hc.executeMethod(getmethod);
		str = IOUtils.toString(getmethod.getResponseBodyAsStream());
		getmethod.releaseConnection();
		return str;
	}
	public static void main(String[] args) throws Exception{
		
//		fetchIp(1);
		HtmlPage page = wc.getPage("http://1caifu.com/1_4885.html");
		System.out.println(page.asXml());
		FpNewProductDTO initInfo = initInfo(page);
//		//大小配比
//		String pSizeRatioType=getText(page.getDocumentElement(),"/html/body/div[1]/div/div[2]/div[1]/div/span[2]");
//		System.out.println(pSizeRatioType);
//		System.out.println(getMethod("http://1212.ip138.com/ic.asp", null));
//		HtmlPage page = wc.getPage("http://www.google.com");
//		System.out.println(page.asXml());
		
		//壹财富登录
//		log.info(LoginYCF("13052357696","6d112c7a57edfb468ea8135b7d19bb9f"));
		//赋值cookie
//		httpToWebClient(hc, wc);
		//壹财富数据      预售和在售
//		String ycfTrustUrl="http://1caifu.com/trust/1/?issaleses=10,20";
//		String urlDetailRegex="/.+_.+\\.html";
//		//获取到所有的详情页地址
//		List<HtmlAnchor> s=getListUrl(ycfTrustUrl,urlDetailRegex);
//		//获取详情
//		for (HtmlAnchor htmlAnchor : s) {
//			HtmlPage click = htmlAnchor.click();
//			//获取产品的信息
//			initInfo(click);
//			//获取到产品信息 ，进行数据库保存。
//			//根据  click.getUrl()  判断是否存在产品
//			
//		}
	}
	
	public static HtmlPage getPage(String url) throws FailingHttpStatusCodeException, MalformedURLException, IOException{
		
		HtmlPage page=null;
		try {
			page = wc.getPage(url);
		} catch (Exception e) {
			log.error("ip  Invalid");
			getMemcache();
			return getPage(url);
		}
		return page;
	}
	
	public static HtmlPage click(HtmlAnchor ha) throws FailingHttpStatusCodeException, MalformedURLException, IOException{
		HtmlPage page=null;
		try {
			log.info(ha.asXml());
			page = ha.click();
		} catch (Exception e) {
			log.error("ip  Invalid");
			getMemcache();
			return click(ha);
		}
		return page;
	}
	/**
	 * 
	
	* @Title: initInfo
	
	* @Description: 通过页面获取产品信息
	
	* @param @param click    设定文件
	
	* @return void    返回类型
	
	* @throws
	 */
	public static FpNewProductDTO initInfo(HtmlPage click) {
		//获取详情页产品的信息   
		FpNewProductDTO fp=new FpNewProductDTO();
		//此处理信托 资管 
		fp.setProductUrl(click.getUrl().toString());
		//简称
		String name=getText(click.getDocumentElement(), "/html/body/div[1]/div/div[2]/div[1]/h3/span[1]");
		fp.setpName(name);
		
		//大小配比
		String pSizeRatioType=getText(click.getDocumentElement(),"/html/body/div[1]/div/div[2]/div[1]/div/span[2]");
		fp.setpSizeRatioType(pSizeRatioType);
		
		//募集说明
		String pRecruitmentSummary=getText(click.getDocumentElement(), "/html/body/div[1]/div/div[2]/div[1]/div/div/div[1]/div/p");
		fp.setpRecruitmentSummary(pRecruitmentSummary);
		//需做特殊处理    预期收益
		try {
			String productAnnual=getText(click.getDocumentElement(),"/html/body/div[1]/div/div[2]/div[1]/div/table/tbody");
			fp.setProductAnnual(productAnnual);
		} catch (Exception e) {
			log.error("error productAnnual",e);
		}
		//全称
		String fullName=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[1]/td[2]");
		fp.setpFullName(fullName);
		//状态
		String pSaleStatus=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[1]/td[4]");
		fp.setpSaleStatus(pSaleStatus);
		//发行机构
		String pInvestOwner=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[2]/td[2]");
		fp.setpInvestOwner(pInvestOwner);
		//募集账号
		String pRaiseAccountNo=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[7]/td[2]");
		fp.setpRaiseAccountNo(pRaiseAccountNo);
		//融资主体
		String rzzt=getText(click.getDocumentElement(), "//*[@id=\"rzzt\"]");
		fp.setRzzt(rzzt);
		//资金用途
		String fundInvest=getText(click.getDocumentElement(), "//*[@id=\"fundInvest\"]");
		fp.setFundinvest(fundInvest);
		//还款来源
		String sourceRepayment=getText(click.getDocumentElement(), "//*[@id=\"sourceRepayment\"]");
		fp.setSourceRepayment(sourceRepayment);
		//风控
		String windControl=getText(click.getDocumentElement(), "//*[@id=\"windControl\"]");
		fp.setWindControl(windControl);
		//项目亮点
		String xmld=getText(click.getDocumentElement(), "//*[@id=\"xmld\"]");
		fp.setXmld(xmld);
		//项目亮点
		String contents=getText(click.getDocumentElement(), "//*[@id=\"contents\"]");
		fp.setContents(contents);
		
		//产品type
		String type=getText(click.getDocumentElement(), "//*[@id=\"tis\"]");
		
		//信托资管不同字段获取处理
		if(StringUtils.isNotBlank(type)&&type.contains("信托")){
			//本期起售日
			String pSaleTimeStart=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[2]/td[4]");
			fp.setpSaleTimeStart(pSaleTimeStart);
			//产品期限
			String pDueTime=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[3]/td[2]");
			fp.setpDueTime(pDueTime);
			//付息方式
			String pPaymentInterestType=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[3]/td[4]");
			fp.setpPaymentInterestType(pPaymentInterestType);
			//产品类别
			String pType=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[4]/td[2]");
			fp.setpType(pType);
			//投资领域
			String pInvestType=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[4]/td[4]");
			fp.setpInvestType(pInvestType);
			//规模
			String pIssuingScale=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[5]/td[2]");
			fp.setpIssuingScale(pIssuingScale);
			//产品起够金额
			String pMinimumAmount=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[5]/td[4]");
			fp.setpMinimumAmount(pMinimumAmount);
		}else if(StringUtils.isNotBlank(type)&&type.contains("资管")){//资管
			//本期起售日
			String pSaleTimeStart=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[3]/td[2]");
			fp.setpSaleTimeStart(pSaleTimeStart);
			//产品期限
			String pDueTime=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[3]/td[4]");
			fp.setpDueTime(pDueTime);
			//付息方式
			String pPaymentInterestType=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[4]/td[2]");
			fp.setpPaymentInterestType(pPaymentInterestType);
			//产品类别
			String pType=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[4]/td[4]");
			fp.setpType(pType);
			//投资领域
			String pInvestType=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[5]/td[2]");
			fp.setpInvestType(pInvestType);
			//规模
			String pIssuingScale=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[5]/td[4]");
			fp.setpIssuingScale(pIssuingScale);
			//产品起够金额
			String pMinimumAmount=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[6]/td[2]");
			fp.setpMinimumAmount(pMinimumAmount);
		}else{//债权基金
			//本期起售日
			String pSaleTimeStart=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[2]/td[4]");
			fp.setpSaleTimeStart(pSaleTimeStart);
			//产品期限
			String pDueTime=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[3]/td[2]");
			fp.setpDueTime(pDueTime);
			//付息方式
			String pPaymentInterestType=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[3]/td[4]");
			fp.setpPaymentInterestType(pPaymentInterestType);
			//产品类别
			String pType=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[4]/td[2]");
			fp.setpType(pType);
			//规模
			String pIssuingScale=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[4]/td[4]");
			fp.setpIssuingScale(pIssuingScale);
			//产品起够金额
			String pMinimumAmount=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[5]/td[2]");
			fp.setpMinimumAmount(pMinimumAmount);
			
		}
		
		return fp;
	}
	
	
	/**
	 * 
	
	* @Title: initInfo20160612
	
	* @Description: 通过页面获取产品信息   改版
	
	* @param @param click    设定文件
	
	* @return void    返回类型
	
	* @throws
	 */
	public static FpNewProductDTO initInfo20160612(HtmlPage click) {
		//获取详情页产品的信息   
		FpNewProductDTO fp=new FpNewProductDTO();
		//此处理信托 资管 
		fp.setProductUrl(click.getUrl().toString());
		//简称
		String name=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[1]/div[2]/div[2]/div[1]/span[1]");
		fp.setpName(name);
		
		//大小配比
		String pSizeRatioType=getText(click.getDocumentElement(),"//*[@id=\"product-detail-container\"]/div[1]/div[1]/div[2]/div[2]/ul/li[4]/p[1]");
		fp.setpSizeRatioType(pSizeRatioType);
		
		//募集说明
		String pRecruitmentSummary=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[1]/div[4]/div[2]/span");
		fp.setpRecruitmentSummary(pRecruitmentSummary);
		//需做特殊处理    预期收益
		try {
			String productAnnual=((HtmlTableBody)click.getFirstByXPath("//*[@id=\"product-detail-container\"]/div[1]/div[1]/div[3]/table/tbody")).asXml();
			fp.setProductAnnual(productAnnual);
		} catch (Exception e) {
			log.error("error productAnnual",e);
		}
		//全称
		String fullName=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[1]/td[2]");
		fp.setpFullName(fullName);
		//状态
		String pSaleStatus=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[1]/td[4]");
		fp.setpSaleStatus(pSaleStatus);
		//发行机构
		String pInvestOwner=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[2]/td[2]");
		fp.setpInvestOwner(pInvestOwner);
		//募集账号
		String pRaiseAccountNo=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/div[3]/table/tbody/tr[1]/td[2]");
		fp.setpRaiseAccountNo(pRaiseAccountNo);
		//融资主体
		String rzzt=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/div[3]/table/tbody/tr[2]/td[2]");
		fp.setRzzt(rzzt);
		//资金用途
		String fundInvest=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/div[3]/table/tbody/tr[3]/td[2]");
		fp.setFundinvest(fundInvest);
		//还款来源
		String sourceRepayment=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/div[3]/table/tbody/tr[4]/td[2]");
		fp.setSourceRepayment(sourceRepayment);
		//风控
		String windControl=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/div[3]/table/tbody/tr[5]/td[2]");
		fp.setWindControl(windControl);
		//项目亮点
		String xmld=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/div[3]/table/tbody/tr[6]/td[2]");
		fp.setXmld(xmld);
		//说明
		String contents=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/div[3]/table/tbody/tr[7]/td[2]");
		fp.setContents(contents);
		
		//产品type
		String type=getText(click.getDocumentElement(), "//*[@id=\"tis\"]");
		
		//信托资管不同字段获取处理
		if(StringUtils.isNotBlank(type)&&type.contains("信托")){
			//本期起售日
			String pSaleTimeStart=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[2]/td[4]");
			fp.setpSaleTimeStart(pSaleTimeStart);
			//产品期限
			String pDueTime=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[3]/td[2]");
			fp.setpDueTime(pDueTime);
			//付息方式
			String pPaymentInterestType=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[3]/td[4]");
			fp.setpPaymentInterestType(pPaymentInterestType);
			//产品类别
			String pType=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[4]/td[2]");
			fp.setpType(pType);
			//投资领域
			String pInvestType=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[4]/td[4]");
			fp.setpInvestType(pInvestType);
			//规模
			String pIssuingScale=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[5]/td[2]");
			fp.setpIssuingScale(pIssuingScale);
			//产品起够金额
			String pMinimumAmount=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[5]/td[4]");
			fp.setpMinimumAmount(pMinimumAmount);
		}else if(StringUtils.isNotBlank(type)&&type.contains("资管")){//资管
			//本期起售日
			String pSaleTimeStart=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[2]/td[4]");
			fp.setpSaleTimeStart(pSaleTimeStart);
			//产品期限
			String pDueTime=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[3]/td[2]");
			fp.setpDueTime(pDueTime);
			//付息方式
			String pPaymentInterestType=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[3]/td[4]");
			fp.setpPaymentInterestType(pPaymentInterestType);
			//产品类别
			String pType=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[4]/td[2]");
			fp.setpType(pType);
			//投资领域
			String pInvestType=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[4]/td[4]");
			fp.setpInvestType(pInvestType);
			//规模
			String pIssuingScale=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[5]/td[2]");
			fp.setpIssuingScale(pIssuingScale);
			//产品起够金额
			String pMinimumAmount=getText(click.getDocumentElement(), "//*[@id=\"product-detail-container\"]/div[1]/div[2]/table/tbody/tr[5]/td[4]");
			fp.setpMinimumAmount(pMinimumAmount);
		}else{
//			//债权基金
//			//本期起售日
//			String pSaleTimeStart=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[2]/td[4]");
//			fp.setpSaleTimeStart(pSaleTimeStart);
//			//产品期限
//			String pDueTime=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[3]/td[2]");
//			fp.setpDueTime(pDueTime);
//			//付息方式
//			String pPaymentInterestType=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[3]/td[4]");
//			fp.setpPaymentInterestType(pPaymentInterestType);
//			//产品类别
//			String pType=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[4]/td[2]");
//			fp.setpType(pType);
//			//规模
//			String pIssuingScale=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[4]/td[4]");
//			fp.setpIssuingScale(pIssuingScale);
//			//产品起够金额
//			String pMinimumAmount=getText(click.getDocumentElement(), "//*[@id=\"prdetail\"]/table/tbody/tr[5]/td[2]");
//			fp.setpMinimumAmount(pMinimumAmount);
		}
		
		return fp;
	}
	
	/**
	 * 
	
	* @Title: initInfo20160713
	
	* @Description: 通过页面获取产品信息   改版
	
	* @param @param click    设定文件
	
	* @return void    返回类型
	
	* @throws
	 */
	public static FpNewProductDTO initInfo20160713(HtmlPage click,Map<String,String> hm) {
		//获取详情页产品的信息   
		FpNewProductDTO fp=new FpNewProductDTO();
		//根据 xpath获取内容
		setNewProductDTO(click, fp,hm);
		
		return fp;
	}

	public static void setNewProductDTO(HtmlPage click, FpNewProductDTO fp,Map<String,String> hm) {
		//此处理信托 资管 
		fp.setProductUrl(click.getUrl().toString());
		//简称
		String name=getText(click.getDocumentElement(), hm.get("pNameXpath"));
		fp.setpName(name);
		
		String pSizeRatioType=getText(click.getDocumentElement(),hm.get("pSizeRatioXpath"));
		fp.setpSizeRatioType(pSizeRatioType);
		
		String pRecruitmentSummary=getText(click.getDocumentElement(), hm.get("pRecruitmentSummaryXpath"));
		fp.setpRecruitmentSummary(pRecruitmentSummary);
		//需做特殊处理    预期收益
		try {
			String productAnnual=((HtmlTable)click.getFirstByXPath(hm.get("productAnnualXpath"))).asXml();
			fp.setProductAnnual(productAnnual);
		} catch (Exception e) {
			log.error("error productAnnual",e);
		}
		String fullName=getText(click.getDocumentElement(), hm.get("fullNameXpath"));
		fp.setpFullName(fullName);
		String pSaleStatus=getText(click.getDocumentElement(), hm.get("pSaleStatusXpath"));
		fp.setpSaleStatus(pSaleStatus);
		String pInvestOwner=getText(click.getDocumentElement(), hm.get("pInvestOwnerXpath"));
		fp.setpInvestOwner(pInvestOwner);
		String pRaiseAccountNo=getText(click.getDocumentElement(), hm.get("pRaiseAccountNoXpath"));
		fp.setpRaiseAccountNo(pRaiseAccountNo);
		String rzzt=getText(click.getDocumentElement(), hm.get("rzztXpath"));
		fp.setRzzt(rzzt);
		String fundInvest=getText(click.getDocumentElement(),hm.get("fundInvestXpath"));
		fp.setFundinvest(fundInvest);
		String sourceRepayment=getText(click.getDocumentElement(), hm.get("sourceRepaymentXpath"));
		fp.setSourceRepayment(sourceRepayment);
		String windControl=getText(click.getDocumentElement(), hm.get("windControlXpath"));
		fp.setWindControl(windControl);
		String xmld=getText(click.getDocumentElement(), hm.get("xmldXpath"));
		fp.setXmld(xmld);
		String contents=getText(click.getDocumentElement(), hm.get("contentsXpath"));
		fp.setContents(contents);
		String pSaleTimeStart=getText(click.getDocumentElement(), hm.get("pSaleTimeStartXpath"));
		fp.setpSaleTimeStart(pSaleTimeStart);
		String pDueTime=getText(click.getDocumentElement(), hm.get("pDueTimeXpath"));
		fp.setpDueTime(pDueTime);
		String pPaymentInterestType=getText(click.getDocumentElement(), hm.get("pPaymentInterestTypeXpath"));
		fp.setpPaymentInterestType(pPaymentInterestType);
		String pType=getText(click.getDocumentElement(), hm.get("pTypeXpath"));
		fp.setpType(pType);
		String pInvestType=getText(click.getDocumentElement(), hm.get("pInvestTypeXpath"));
		fp.setpInvestType(pInvestType);
		String pIssuingScale=getText(click.getDocumentElement(),hm.get("pIssuingScaleXpath"));
		fp.setpIssuingScale(pIssuingScale);
		String pMinimumAmount=getText(click.getDocumentElement(), hm.get("pMinimumAmountXpath"));
		fp.setpMinimumAmount(pMinimumAmount);
	}
	
	//抓取所有的标签   
	public static List<HtmlAnchor> getListUrl(String ycfTrustUrl,String urlDetailRegex) throws Exception{
		String nextPage=null;
		List<HtmlAnchor> anchor=new ArrayList<HtmlAnchor>();
		HtmlPage page = null ;
		do{
			if(StringUtils.isBlank(nextPage)){
//				page=wc.getPage(ycfTrustUrl);
				page=getPage(ycfTrustUrl);
			}else{
				//单机下一页
//				page=wc.getPage(nextPage);
				page=getPage(nextPage);
			}
			
			//当前页
			log.info("current page:"+page.getUrl().toString());
			try {
				HtmlAnchor nextP=page.getAnchorByText("下一页");
				String next=fecth(nextP==null?"":nextP.asXml(),".*?(/\\d+/).*?",1);
				if(!StringUtils.isBlank(next)){
					//获取下一页的标签
					nextPage = ycfTrustUrl.replaceAll("/\\d+/",next);
				}
			} catch (Exception e) {
				try {
					HtmlAnchor nextP=page.getAnchorByText(">");
					String next=fecth(nextP==null?"":nextP.asXml(),".*?(/\\d+/).*?",1);
					if(!StringUtils.isBlank(next)){
						//获取下一页的标签
						nextPage = ycfTrustUrl.replaceAll("/\\d+/",next);
					}
				} catch (Exception e1) {
					nextPage="";
					log.info("no has next page url");
				}
			}
			
			// 得到下一页的地址
			List<HtmlAnchor> a = page.getAnchors();
			for (HtmlAnchor htmlAnchor : a) {
				if(StringUtils.isNotBlank(htmlAnchor.getHrefAttribute())&&htmlAnchor.getHrefAttribute().matches(urlDetailRegex)){
					System.out.println(htmlAnchor.getHrefAttribute());
					anchor.add(htmlAnchor);
				}
			}
			wc.closeAllWindows();
			if(flagTest){
				log.info("测试，关闭下一页查询。"+nextPage);
				nextPage=null; 
			}
		}while(!StringUtils.isBlank(nextPage));
		return anchor;
	}
	
	
	/**
	 * 
	
	* @Title: LoginYCF
	
	* @Description: 登录壹财富
	
	* @param @param username
	* @param @param password
	* @param @return
	* @param @throws IOException    设定文件
	
	* @return boolean    返回类型
	
	* @throws
	 */
	public static boolean LoginYCF(String username,String password)
			throws IOException {
		Map<String,String> params=new HashMap<String, String>();
		params.put("username", username);
		params.put("password", password);
		String s= postMethod("http://1caifu.com/api/user/login", params);
		if(s.contains("isSuccess\":true")){
			log.info("login success");
			return true;
		}
		log.info("login fail");
		return false;
	}
	
}
